package com.aplink.generic.pojo.controlspec.actionbar;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.sg.R;

public class ActionBarSpec extends ControlSpec {
    private static final long serialVersionUID = 8741632117660221307L;
    private boolean isDisplayHomeAsUp;
    private GenericDictionary<String, MenuItemSpec> mButtons;
    private String mIcon;
    private GenericDictionary<String, MenuItemSpec> mOptionMenuItems;
    private SearchViewSpec mSearchViewSpec;
    private SelectionSpec mSelectionSpec;
    private String mSubTitle;
    private String mTitle;
    private boolean isDisplayShowHomeEnabled;

    public ActionBarSpec() {
        this.mOptionMenuItems = new GenericDictionary<>();
        this.mButtons = new GenericDictionary<>();
    }

    public void add(final MenuItemSpec menuItemSpec) {
        this.mOptionMenuItems.put(menuItemSpec.getID(), menuItemSpec);
    }

    public void addButton(final MenuItemSpec menuItemSpec) {
        this.mButtons.put(menuItemSpec.getID(), menuItemSpec);
    }

    public LinearLayout getActionBarButton(final GenericFragment genericFragment) {
        final Activity activity = genericFragment.getActivity();
        final LinearLayout root = (LinearLayout) activity.getLayoutInflater()
                .inflate(R.layout.aplink__custom_actionbar,
                        new LinearLayout(activity), false);
        for (int i = 0; i < this.mButtons.size(); i++) {
            final LinearLayout linearLayout = (LinearLayout) activity
                    .getLayoutInflater().inflate(
                            R.layout.aplink__custom_actionbar_button,
                            new LinearLayout(activity), false);
            final MenuItemSpec menuItemSpec = this.mButtons.valueAt(i);
            if (menuItemSpec.getIconID() != ResourcesHelper.RESOURCE_NOT_FOUND) {
                final ImageView imageView = (ImageView) activity
                        .getLayoutInflater().inflate(
                                R.layout.aplink__custom_actionbar_button_image,
                                new LinearLayout(activity), false);
                imageView.setImageResource(menuItemSpec.getIconID());
                linearLayout.addView(imageView);
            }
            if (!TextUtils.isEmpty(menuItemSpec.getTitle())) {
                final TextView textView = (TextView) activity
                        .getLayoutInflater().inflate(
                                R.layout.aplink__custom_actionbar_button_text,
                                new LinearLayout(activity), false);
                textView.setText(menuItemSpec.getTitle());
                linearLayout.addView(textView);
            }
            linearLayout.setTag(menuItemSpec);
            linearLayout.setOnClickListener(genericFragment
                    .getOnActionbarButtonClickListener());
            root.addView(linearLayout);
        }
        return root;
    }

    public GenericDictionary<String, MenuItemSpec> getButtons() {
        return this.mButtons;
    }

    public String getIcon() {
        return this.mIcon;
    }

    public int getIconID() {
        if (TextUtils.isEmpty(this.mTitle)) {
            return ResourcesHelper.RESOURCE_NOT_FOUND;
        }
        return ResourcesHelper.getDrawableIdByName(this.mIcon);
    }

    public GenericDictionary<String, MenuItemSpec> getOptionMenuItems() {
        return this.mOptionMenuItems;
    }

    public SearchViewSpec getSearchViewSpec() {
        return this.mSearchViewSpec;
    }

    public SelectionSpec getSelectionSpec() {
        return this.mSelectionSpec;
    }

    public String getSubTitle() {
        return this.mSubTitle;
    }

    public String getTitle() {
        return this.mTitle;
    }

    public boolean isDisplayHomeAsUp() {
        return this.isDisplayHomeAsUp;
    }

    public void setButtons(final GenericDictionary<String, MenuItemSpec> buttons) {
        this.mButtons = buttons;
    }

    public void setDisplayHomeAsUp(final boolean isDisplayHomeAsUp) {
        this.isDisplayHomeAsUp = isDisplayHomeAsUp;
    }

    public void setIcon(final String icon) {
        this.mIcon = icon;
    }

    public void setOptionMenuItems(
            final GenericDictionary<String, MenuItemSpec> optionMenuItems) {
        this.mOptionMenuItems = optionMenuItems;
    }

    public void setSearchViewSpec(final SearchViewSpec searchViewSpec) {
        this.mSearchViewSpec = searchViewSpec;
    }

    public void setSelectionSpec(final SelectionSpec selectionSpec) {
        this.mSelectionSpec = selectionSpec;
    }

    public void setSubTitle(final String subTitle) {
        this.mSubTitle = subTitle;
    }

    public void setTitle(final String title) {
        this.mTitle = title;
    }

    public boolean isDisplayShowHomeEnabled() {
        return isDisplayShowHomeEnabled;
    }

    public void setDisplayShowHomeEnabled(boolean isDisplayShowHomeEnabled) {
        this.isDisplayShowHomeEnabled = isDisplayShowHomeEnabled;
    }
}
